/**
 *  Stack - LinkedList Implementation
 *
 */

public class StackLinkedList<T> {
    
    private int total;

    private Node head;

    private class Node {
        private T item;
        private Node next;
    }

    public StackLinkedList() {}

    public void push(T item) {
        Node current = head;
        head = new Node();
        head.item = item;
        head.next = current;
        total++;
    }

    public T pop() {
        if (total == 0) {
            return null;
        }

        T item = head.item;
        head = head.next;
        total--;

        return item;
    }

    public int size() {
        return total;
    }

    public T peek() {
        if (total == 0) {
            return null;
        }

        return head.item;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node tmp = head;
        while (tmp != null) {
            sb.append(tmp.item).append(", ");
            tmp = tmp.next;
        }
        return sb.toString();
    }

}
